﻿using System;
using System.CodeDom.Compiler;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FBGraph.ClientGenerator.CodeGen;

namespace FBGraph.ClientGenerator.Tests.CodeGen
{
    [TestClass]
    public class PropertyTests
    {
        [TestMethod]
        public void OutputBasic()
        {
            var prop = new Property("Foo", typeof(String))
            {
                HasGet = true,
                HasSet = true,
                GetBody = "return _foo;",
                SetBody = "_foo = value;"
            };
            var expected =
@"public String Foo
{
    get
    {
        return _foo;
    }
    set
    {
        _foo = value;
    }
}
".ReplaceTabs();
            var actual = prop.ToString();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void OutputAccessorsWithDifferentAccessibility()
        {
            var prop = new Property("Foo", typeof(String))
            {
                HasGet = true,
                HasSet = true,
                GetBody = "return _foo;",
                SetBody = "_foo = value;",
                SetAccessibility = "protected"
            };
            var expected =
@"public String Foo
{
    get
    {
        return _foo;
    }
    protected set
    {
        _foo = value;
    }
}
".ReplaceTabs();
            var actual = prop.ToString();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void OutputSingleAccessor()
        {
            var prop = new Property("Foo", typeof(String))
            {
                HasGet = true,
                GetBody = "return _foo;"
            };
            var expected =
@"public String Foo
{
    get
    {
        return _foo;
    }
}
".ReplaceTabs();
            var actual = prop.ToString();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void OutputEmptyAccessor()
        {
            var prop = new Property("Foo", typeof(String))
            {
                HasGet = true,
                HasSet = true,
                GetBody = "return _foo;",
                SetBody = String.Empty,
                SetAccessibility = "protected"
            };
            var expected =
@"public String Foo
{
    get
    {
        return _foo;
    }
    protected set { }
}
".ReplaceTabs();
            var actual = prop.ToString();

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SuggestNullifiesValueType()
        {
            var list = new PropertyList(new CodeGenType("TestClass", "Test"), new Property("Foo", typeof(Object)));

            list.Suggest(new Property("Foo", typeof(Int64)));

            Assert.AreEqual(typeof(Int64?), list[0].BaseType.Type);
        }

        [TestMethod]
        public void MakeNullable()
        {
            var expected = new Property("Foo", typeof(Int64?));
            var actual = new Property("Foo", typeof(Object));
            actual.MakeNullable();

            Assert.AreEqual(expected, actual);
        }
    }
}